var buyRecordListUrl = basePath + "/course/json/signOrder/list";
var orderConfirmPayUrl = basePath + "/course/json/order/confirmPay";
var orderRefundUrl = basePath + "/course/json/order/refund";

var dataGrid;
$(function() {
	loadData();
});

function loadData() {
	dataGrid = $('#dataGrid').datagrid({
		url : buyRecordListUrl,
		fit : true,
		fitColumns : true,
		border : true,
		pagination : true,
		idField : 'id',
		pageSize : config.pageSize,
		autoRowHeight : 140,
		pageList : config.pageList,
		checkOnSelect : true,
		selectOnCheck : true,
		singleSelect : false,
		nowrap : true,
		striped : true,
		rownumbers : true,
		columns : [ [  {
			field : 'action',
			title : '操作',
			align : 'center',
			formatter : function(value, row, index) {
				var str = "";
				if(row.orderStatus == '000') {
					if(row.signWay == 1) {
						str += $.formatString('<button  type="button" class="btn btn-success btn-xs" style="margin:4px 4px;" onclick="uploadNamelist(\'{0}\');">上传名单</button>', index);
					}
					str += $.formatString('<button  type="button" class="btn btn-danger btn-xs" style="margin:4px 4px;" onclick="orderRefund(\'{0}\');">退款</button>', row.orderId, index);
				}else if(row.orderStatus == '001') {
					str += $.formatString('<button  type="button" class="btn btn-warning btn-xs" style="margin:4px 4px;" onclick="orderConfirmPay(\'{0}\');">确认支付</button>', row.orderId);
				}
				return str;
			}
		}, {
			field : 'orderStatus',
			title : '订单状态',
			align : "center",
			resizable : true,
			formatter: function(v) {
				if(v == '000') return "<span style='color: green;'>已支付</span>";
				if(v == '001') return "<span style='color: red;'>未支付</span>";
				if(v == '002') return "支付失败";
				if(v == '003') return "支付处理中";
				if(v == '004') return "订单失效";
				if(v == '005') return "退款中";
				if(v == '006') return "已退款";
				return "";
			}
		}, {
			field : 'orderNo',
			title : '订单编号',
			align : "center",
			resizable : true
		}, {
			field : 'signName',
			title : '报名联系人',
			align : "center",
			resizable : true
		}, {
			field : 'signPhone',
			title : '联系电话',
			align : "center",
			resizable : true
		}, {
			field : 'courseName',
			title : '课程名称',
			align : "center",
			resizable : true
		}, {
			field : 'totalPrice',
			title : '总金额',
			align : "center",
			resizable : true
		}, {
			field : 'payAmount',
			title : '支付金额',
			align : "center",
			resizable : true
		}, {
			field : 'exchangePoints',
			title : '抵换积分',
			align : "center",
			resizable : true
		}, {
			field : 'givePoints',
			title : '赠送积分',
			align : "center",
			resizable : true
		}, {
			field : 'orderCreateTime',
			title : '报名时间',
			align : "center",
			resizable : true,
			formatter : function(value) {
				return getTime(value, "yyyy-MM-dd hh:mm:ss");
			}
		}, {
			field : 'paySubmitTime',
			title : '支付成功时间',
			align : "center",
			resizable : true,
			formatter : function(value) {
				return getTime(value, "yyyy-MM-dd hh:mm:ss");
			}
		}] ]
	});
}

function search(formId){
	dataGrid.datagrid('load', $.serializeObject($('#' + formId)));
}

function closeFormPanel(formId){
	cleanFormPanel(formId);
	confirmDialog.dialog("close");
}

function orderConfirmPay(orderId) {
	$.messager.confirm('系统提示', '请再三确认，该订单是否已支付成功？', function(r) {
		if (r) {
			$.ajax({
				url : orderConfirmPayUrl,
				type : 'POST',
				dataType:"json",
				data : {
					orderId: orderId
				},
				error : function() {
					$.messager.alert('系统提示', '确认失败', 'error');
				},
				success : function(data) {
					$.messager.progress('close');
					if (data.code == '000') {
						$.messager.alert('系统提示','确认成功', 'info');
						loadData();
					} else {
						$.messager.alert('系统提示',data.msg, 'warning');
					}
				}
			});
		}
	});
}

function orderRefund(orderId) {
	$.messager.confirm('系统提示', '请再三确认，该订单是否确认退款？', function(r) {
		if (r) {
			$.messager.progress({
				title : '系统提示',
				msg : '处理中，请稍候...'
			});
			$.ajax({
				url : orderRefundUrl,
				type : 'POST',
				dataType:"json",
				data : {
					orderId: orderId
				},
				error : function() {
					$.messager.progress('close');
					$.messager.alert('系统提示', '确认失败', 'error');
				},
				success : function(data) {
					$.messager.progress('close');
					if (data.code == '000') {
						$.messager.alert('系统提示','退款申请提交成功', 'info');
						loadData();
					} else {
						if(data.code == '1024') {
							$.messager.alert('系统提示',data.data, 'warning');
							return;
						}
						$.messager.alert('系统提示',data.msg, 'warning');
					}
				}
			});
		}
	});
}

/**
 * 清空表单
 */
var cleanFormPanel=function(formId){
	$('#' + formId)[0].reset();
}

function reportExport(form){
	$("#" + form).attr('action',basePath + '/course/signOrder/export').submit();
}
